package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.ThirdAdminMapper;
import com.gxa.insurance.pojo.InsureApply;
import com.gxa.insurance.pojo.ThirdAdmin;
import com.gxa.insurance.service.ThirdAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 10076
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class ThirdAdminServiceImpl implements ThirdAdminService {
    @Autowired
    ThirdAdminMapper thirdAdminMapper;
    @Override
    public ThirdAdmin selectById(Integer id) throws Exception {
        if (id==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        return thirdAdminMapper.selectById(id);
    }



    @Override
    public List<Map<String, Object>> selectByPage(Integer page, Integer size) {
        page =(page-1)*size;
        return  thirdAdminMapper.selectByPage(page,size);
    }

    @Override
    public List<ThirdAdmin> selectAll() throws BusinessException {

        return thirdAdminMapper.selectAll();
    }

    @Override
    public List<InsureApply> selectAllApply() {
        return thirdAdminMapper.selectAllApply();
    }

    @Override
    public List<String> selectAllByname(){

        List<String> name = new ArrayList<>();
        List<ThirdAdmin> thirdAdmins = thirdAdminMapper.selectAllByname();
        // 提取每个对象中的名字
        for (ThirdAdmin admin : thirdAdmins) {
            name.add(admin.getName());
        }
        return name;
    }

    @Override
    public Integer insert(ThirdAdmin thirdAdmin) throws BusinessException {
        if(thirdAdmin==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
       return thirdAdminMapper.insert(thirdAdmin);
    }

    @Override
    public Integer insertApply(InsureApply insureApply) throws BusinessException {
        if(insureApply==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        return thirdAdminMapper.insertApply(insureApply);
    }

    @Override
    public void delect(Integer id) throws BusinessException {
        if(id==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
        thirdAdminMapper.delete(id);
    }

    @Override
    public void update(ThirdAdmin thirdAdmin) throws BusinessException {
        if(thirdAdmin==null){
            throw new BusinessException(ExceptionCode.USER_NOT_FONUD);
        }
            thirdAdminMapper.update(thirdAdmin);
    }


}
